package com.android.Bean;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.android.DBTools.ADUS;
import com.android.DBTools.DBConnection;

//数据库为 generalusers
public class LoginCheckBean {

	// 判断用户账号信息 返回值整型：-1 为错误账号 0为企业账号 1为物业管理 登录验证
	public int checkUsers(String username, String password) throws SQLException, ClassNotFoundException {
		String sql = "select * from generalusers where " + username + " = genLogin and " + password + " =genPassword;";
		ResultSet rs = ADUS.selectData(sql);
		if (rs.next()) {
			int role = rs.getInt(rs.findColumn("role"));
			DBConnection.closeCon();
			return role;
		} else {
			return -1;
		}
	}

	// 注册企业账号
	public boolean loginUser(String username, String password) throws SQLException, ClassNotFoundException {
		if (username.length() > 15 || password.length() > 15) {
			return false;
		}
		String sql = "select * from generalusers where " + username + " = genLogin;";
		ResultSet rs = ADUS.selectData(sql);
		if (rs.next()) {
			DBConnection.closeCon();
			return false;// 账号被占用
		} else {
			sql = "INSERT INTO generalusers (genUID,genLogin,genPassword,role) VALUES (null,\"" + username + "\",\""
					+ password + "\",0)";
			System.out.println(sql);
			ADUS.insertData(sql);
			DBConnection.closeCon();
			return true;// 注册成功
		}

	}
}
